﻿using Prism.Mvvm;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace WpfApp_SmartParking_Models;

public class UserModel : BindableBase, INotifyDataErrorInfo
{

    private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();

    public bool HasErrors => _errors.Count > 0;

    public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;

    public int Id { get; set; }

    private string? _Name;
    public int? CompanyId { get; set; }

    public string? Password { get; set; }

    public int Status { get; set; }

    public string? Phone { get; set; }

    public string? Mobile { get; set; }

    public string? Address { get; set; }

    public string? Email { get; set; }

    public string? Qq { get; set; }

    public string? Avatar { get; set; }

    public int? Gender { get; set; }





    public string? Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            ValidateName();
        }
    }

    public void ValidateName()
    {
        _errors.Remove(nameof(this.Name));
        if (string.IsNullOrEmpty(this.Name)) 
        {
            AddError(nameof(this.Name), "姓名不能为空！");
            return;
        }
        if (this.Name == "111") 
        {
            AddError(nameof(this.Name), "姓名错误！");
            return;
        }
    }

    private void AddError(string propertyName,string errorInfo)
    {
        _errors.Add(propertyName, new List<string>() { errorInfo });
        ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
    }

    public IEnumerable GetErrors(string? propertyName)
    {
        if (propertyName != null && _errors.ContainsKey(propertyName))
        {
            return _errors[propertyName];
        }
        return Enumerable.Empty<string>();
    }



}
